﻿using Apps.Common;
using Apps.Models;
using Apps.Web.Core;
using Apps.Web.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Apps.Web.Controllers
{
    public partial class HomeController : BaseController
    {
        public ActionResult Gauge(string title, string value, int min, int max)
        {
            ViewBag.title = title;
            ViewBag.value = value;
            ViewBag.min = min;
            ViewBag.max = max;

            return View();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="QueryType">0=按年查询;1=按月查询</param>
        /// <param name="QueryYear"></param>
        /// <param name="QueryMonth"></param>
        /// <returns></returns>
        public ActionResult BarLabelRotation(int QueryType, int QueryYear, int QueryMonth)
        {
            List<string> xAxisList = new List<string>();
            List<object> BlanceList = new List<object>();
            List<object> ExpendList = new List<object>();
            List<object> StockList = new List<object>();
            if (QueryType == 0)
            {
                List<V_RPT_JHC_MONTH> list = homeBLL.QueryJHCMonth(QueryYear);
                for (var idx = 1; idx < 13; idx++)
                {
                    xAxisList.Add(string.Format("{0}月", idx));
                    var item = (from u in list where u.RPT_MONTH == idx select u).FirstOrDefault();
                    if (item != null)
                    {
                        BlanceList.Add(item.RPT_IN);
                        ExpendList.Add(item.RPT_OUT);
                        StockList.Add(item.RPT_STOCK);
                    }
                    else
                    {
                        BlanceList.Add("");
                        ExpendList.Add("");
                        StockList.Add("");
                    }
                }
            }
            else
            {
                List<V_RPT_JHC_DAY> list = homeBLL.QueryJHCDay(QueryYear, QueryMonth);
                DateTime dtStart = Convert.ToDateTime(string.Format("{0}/{1}/01", QueryYear, QueryMonth));
                DateTime dtEnd = Convert.ToDateTime(string.Format("{0}/{1}/01", QueryYear, QueryMonth)).AddMonths(1);
                while (dtStart < dtEnd)
                {
                    xAxisList.Add(dtStart.ToString("dd"));

                    int iDay = dtStart.Day;
                    DateTime Date = DateTime.Parse(string.Format("{0}/{1}/{2}", QueryYear, QueryMonth, iDay));
                    var item = (from u in list where u.RPT_DATE == Date select u).FirstOrDefault();
                    if (item != null)
                    {
                        BlanceList.Add(item.RPT_IN);
                        ExpendList.Add(item.RPT_OUT);
                        StockList.Add(item.RPT_STOCK);
                    }
                    else
                    {
                        BlanceList.Add("");
                        ExpendList.Add("");
                        StockList.Add("");
                    }

                    dtStart = dtStart.AddDays(1);
                }
            }

            BarLabelRotationModel model = new BarLabelRotationModel();
            model.Series = new List<BarLabelRotationSeriesModel>();
            model.Legend = string.Format("['进煤量',  '耗煤量','{0}']", QueryType == 0 ? "月末库存" : "当日库存");
            model.xAxisData = Newtonsoft.Json.JsonConvert.SerializeObject(xAxisList);

            BarLabelRotationSeriesModel Series = new BarLabelRotationSeriesModel();
            Series.name = "进煤量";
            Series.data = Newtonsoft.Json.JsonConvert.SerializeObject(BlanceList);
            model.Series.Add(Series);

            //Series = new BarLabelRotationSeriesModel();
            //Series.name = "耗煤量";
            //Series.data = Newtonsoft.Json.JsonConvert.SerializeObject(ExpendList);
            //model.Series.Add(Series);

            //Series = new BarLabelRotationSeriesModel();
            //Series.name = QueryType == 0 ? "月末库存" : "当日库存";
            //Series.data = Newtonsoft.Json.JsonConvert.SerializeObject(StockList);
            //model.Series.Add(Series);

            return View(model);
        }

        public ActionResult BarLabelRotation2(int QueryType, int QueryYear, int QueryMonth)
        {
            List<V_RPT_RCM_DETAIL> list = new List<V_RPT_RCM_DETAIL>();
            List<string> xAxisList = new List<string>();
            List<object> BlanceList = new List<object>();
            List<object> CarList = new List<object>();
            if (QueryType == 0)
            {
                list = homeBLL.QueryRCMDetail(QueryYear);
                for (var idx = 1; idx < 13; idx++)
                {
                    xAxisList.Add(string.Format("{0}月", idx));
                    var item = (from u in list where u.STATS_MONTH == idx select u).FirstOrDefault();
                    if (item != null)
                    {
                        BlanceList.Add(item.BALANCE);
                        CarList.Add(item.CUTBILL);
                    }
                    else
                    {
                        BlanceList.Add("");
                        CarList.Add("");
                    }
                }
            }
            else
            {
                list = homeBLL.QueryRCMDetail(QueryYear, QueryMonth);
                DateTime dtStart = Convert.ToDateTime(string.Format("{0}-{1}-01", QueryYear, QueryMonth));
                DateTime dtEnd = Convert.ToDateTime(string.Format("{0}-{1}-01", QueryYear, QueryMonth)).AddMonths(1);
                while (dtStart < dtEnd)
                {
                    xAxisList.Add(dtStart.ToString("dd"));

                    int iDay = dtStart.Day;
                    var item = (from u in list where u.STATS_DAY == iDay select u).FirstOrDefault();
                    if (item != null)
                    {
                        BlanceList.Add(item.BALANCE);
                        CarList.Add(item.CUTBILL);
                    }
                    else
                    {
                        BlanceList.Add("");
                        CarList.Add("");
                    }

                    dtStart = dtStart.AddDays(1);
                }
            }

            BarLabelRotationModel model = new BarLabelRotationModel();
            model.Series = new List<BarLabelRotationSeriesModel>();
            model.Legend = "['总量',  '车数']";

            model.xAxisData = Newtonsoft.Json.JsonConvert.SerializeObject(xAxisList);

            BarLabelRotationSeriesModel Series = new BarLabelRotationSeriesModel();
            Series.name = "总量";
            Series.data = Newtonsoft.Json.JsonConvert.SerializeObject(BlanceList);
            model.Series.Add(Series);

            Series = new BarLabelRotationSeriesModel();
            Series.name = "车数";
            Series.data = Newtonsoft.Json.JsonConvert.SerializeObject(CarList);
            model.Series.Add(Series);

            return View(model);
        }

        public ActionResult BarLabelRotation3()
        {
            var list = homeBLL.QueryCoalStructure();

            List<string> ltDay = new List<string>();
            List<decimal> ltTotal = new List<decimal>();
            Random rnd = new Random();
            foreach (var item in list)
            {
                ltDay.Add(item.COALYARD_NAME);
                ltTotal.Add(item.TOTAL);
            }

            ViewBag.CoalStructure = Newtonsoft.Json.JsonConvert.SerializeObject(list);
            ViewBag.Day = Newtonsoft.Json.JsonConvert.SerializeObject(ltDay);
            ViewBag.Total = Newtonsoft.Json.JsonConvert.SerializeObject(ltTotal);

            return View();
        }

        public ActionResult BarLabelRotation4(int QueryType)
        {
            List<string> xAxisList = new List<string>();
            List<object> BlanceList = new List<object>();
            List<object> CarList = new List<object>();
            List<V_RPT_RCM_DETAIL> list = new List<V_RPT_RCM_DETAIL>();
            if (QueryType == 0)
            {
                list = homeBLL.QueryRCMDetail(p => p.STATS_YEAR == DateTime.Now.Year && p.STATS_MONTH == DateTime.Now.Month && p.STATS_DAY == DateTime.Now.Day);
                for (var idx = 0; idx < 24; idx++)
                {
                    xAxisList.Add(string.Format("{0}", idx));
                    var temp = (from u in list where u.STATS_TIME.GetDateTime().Hour == idx select u).ToList();
                    if (temp != null && temp.Count > 0)
                    {
                        decimal dBlance = 0;
                        foreach (var item in temp)
                        {
                            dBlance += item.BALANCE;
                        }

                        BlanceList.Add(dBlance);
                        CarList.Add(temp.Count);
                    }
                    else
                    {
                        BlanceList.Add("");
                        CarList.Add("");
                    }
                }
            }
            else
            {
                int week = (int)DateTime.Now.DayOfWeek;
                list = homeBLL.QueryRCMDetail(p => p.STATS_YEAR == DateTime.Now.Year && p.STATS_MONTH == DateTime.Now.Month && (p.STATS_DAY >= (DateTime.Now.Day - week + 1) && p.STATS_DAY <= (DateTime.Now.Day + 7 - week)));

                for (var idx = 0; idx < 7; idx++)
                {
                    int iDay =DateTime.Now.Day - week + idx + 1;
                    xAxisList.Add(iDay.ToString());

                    var temp = (from u in list where u.STATS_DAY == iDay select u).ToList();
                    if (temp != null && temp.Count > 0)
                    {
                        decimal dBlance = 0;
                        foreach (var item in temp)
                        {
                            dBlance += item.BALANCE;
                        }

                        BlanceList.Add(dBlance);
                        CarList.Add(temp.Count);
                    }
                    else
                    {
                        BlanceList.Add("");
                        CarList.Add("");
                    }
                }
            }

            BarLabelRotationModel model = new BarLabelRotationModel();
            model.Series = new List<BarLabelRotationSeriesModel>();
            model.Legend = "['总量',  '车数']";

            model.xAxisData = Newtonsoft.Json.JsonConvert.SerializeObject(xAxisList);

            BarLabelRotationSeriesModel Series = new BarLabelRotationSeriesModel();
            Series.name = "总量";
            Series.data = Newtonsoft.Json.JsonConvert.SerializeObject(BlanceList);
            model.Series.Add(Series);

            Series = new BarLabelRotationSeriesModel();
            Series.name = "车数";
            Series.data = Newtonsoft.Json.JsonConvert.SerializeObject(CarList);
            model.Series.Add(Series);

            return View(model);
        }


        public ActionResult LevelOne(int QueryType, int QueryYear, int QueryMonth, string Day)
        {
            ViewBag.QueryType = QueryType;
            ViewBag.QueryYear = QueryYear;
            if (QueryType == 0)
            {
                ViewBag.QueryMonth = Convert.ToDateTime(string.Format("{0}年{1}1日", QueryYear, Day)).Month;
                ViewBag.Day = 0;
            }
            else
            {
                ViewBag.QueryMonth = QueryMonth;
                ViewBag.Day = Day;
            }

            return View();
        }

        public ActionResult LevelTwo(string LPR, int QueryYear, int QueryMonth)
        {
            ViewBag.LPR = LPR;
            ViewBag.QueryYear = QueryYear;
            ViewBag.QueryMonth = QueryMonth;

            return View();
        }

        public ActionResult LevelThree(int QueryYear, int QueryMonth, string Day, string MATERIEL_ID, string SUPPLIER_ID, string TRANSPORT_ID)
        {
            ViewBag.QueryYear = QueryYear;
            ViewBag.QueryMonth = QueryMonth;
            ViewBag.Day = Day;

            ViewBag.MATERIEL_ID = MATERIEL_ID;
            ViewBag.SUPPLIER_ID = SUPPLIER_ID;
            ViewBag.TRANSPORT_ID = TRANSPORT_ID;
            return View();
        }


        public JsonResult QueryCALORIFIC()
        {
            return Json(homeBLL.QueryLmsRZC(), JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public JsonResult QueryRCPlanWeighting(GridPager pager, int QueryYear, int QueryMonth, int Day, string MATERIEL_ID, string SUPPLIER_ID, string TRANSPORT_ID)
        {
            List<V_RPT_RCM_DETAIL> list = new List<V_RPT_RCM_DETAIL>();
            if (Day == 0)
            {
                list = homeBLL.QueryRCMDetail(QueryYear, QueryMonth, Day);
            }
            else
             {
                list = homeBLL.QueryRCMDetail(QueryYear, QueryMonth, Day, MATERIEL_ID, SUPPLIER_ID, TRANSPORT_ID);
            }
            GridRows<V_RPT_RCM_DETAIL> grs = new GridRows<V_RPT_RCM_DETAIL>();
            grs.rows = list;
            grs.total = pager.totalRows;
            return Json(grs);
        }

        [HttpPost]
        public JsonResult QueryCarWeighting(GridPager pager, int QueryYear, int QueryMonth, string LPR)
        {
            List<V_RPT_RCM_DETAIL> list = homeBLL.QueryRCMDetailChart(QueryYear, QueryMonth, LPR);
            GridRows<V_RPT_RCM_DETAIL> grs = new GridRows<V_RPT_RCM_DETAIL>();
            grs.rows = list;
            grs.total = pager.totalRows;
            return Json(grs);
        }
        

        [HttpPost]
        public JsonResult QueryRCWeighting(GridPager pager, string BATCH_CODE)
        {
            var list = homeBLL.QueryWeighting(BATCH_CODE);
            GridRows<V_RC_WEIGHING> grs = new GridRows<V_RC_WEIGHING>();
            grs.rows = list;
            grs.total = pager.totalRows;
            return Json(grs);
        }

        public ActionResult LineStack(string MZType,int QueryYear)
        {
            var list = homeBLL.QueryLmsAll(QueryYear, MZType);

            List<string> xAxisList = new List<string>();
            List<object> RCList = new List<object>();
            List<object> RLList = new List<object>();

            for (var idx = 1; idx < 13; idx++)
            {
                xAxisList.Add(string.Format("{0}月", idx));
                var item = (from u in list where u.RPT_MONTH == idx select u).FirstOrDefault();                
                if (item != null)
                {
                    RCList.Add(item.VAL_RC==0? "'-'": item.VAL_RC.ToString());
                    RLList.Add(item.VAL_RL == 0 ? "'-'" : item.VAL_RL.ToString());
                }
                else
                {
                    RCList.Add("");
                    RLList.Add("");
                }
            }

            LineStackModel model = new LineStackModel();
            model.Series = new List<LineStackSeriesModel>();
            model.Legend = "['入厂煤质',  '入炉煤质']";
            model.xAxisData = Newtonsoft.Json.JsonConvert.SerializeObject(xAxisList); 

            LineStackSeriesModel Series = new LineStackSeriesModel();
            Series.name = "入厂煤质";
            Series.data = Newtonsoft.Json.JsonConvert.SerializeObject(RCList); 
            model.Series.Add(Series);

            Series = new LineStackSeriesModel();
            Series.name = "入炉煤质";
            Series.data = Newtonsoft.Json.JsonConvert.SerializeObject(RLList);
            model.Series.Add(Series);

            return View(model);
        }

        public ActionResult LineStack2(string MATERIEL_ID, string SUPPLIER_ID, string TRANSPORT_ID, int QueryYear, int QueryMonth)
        {
            var list = homeBLL.QueryRCMDetailChart(QueryYear, QueryMonth, MATERIEL_ID, SUPPLIER_ID, TRANSPORT_ID);

            DateTime dtStartDate = Convert.ToDateTime(string.Format("{0}-{1}-01", QueryYear, QueryMonth));
            DateTime dtEndDate = Convert.ToDateTime(string.Format("{0}-{1}-01", QueryYear, QueryMonth + 1));

            List<string> xAxisList = new List<string>();
            List<object> BlanceList = new List<object>();
            List<object> BillWeightList = new List<object>();
            List<object> CarCountList = new List<object>();
            while (dtStartDate < dtEndDate)
            {
                int QueryDate = dtStartDate.Day;
                xAxisList.Add(QueryDate.ToString());
                var temp = (from u in list where u.STATS_DAY == QueryDate select u).ToList();
                if (temp != null && temp.Count > 0)
                {
                    decimal dBlance = 0;
                    decimal dWeight = 0;
                    decimal dCount = 0;
                    foreach (var item in temp)
                    {
                        dBlance += item.BALANCE;
                        dWeight += (decimal)item.BILL_WEIGHT;
                        dCount += item.CUTBILL;
                    }

                    BlanceList.Add(dBlance);
                    BillWeightList.Add(dWeight);
                    CarCountList.Add(dCount);
                }
                else
                {
                    BlanceList.Add("");
                    BillWeightList.Add("");
                    CarCountList.Add("");
                }
                dtStartDate = dtStartDate.AddDays(1);
            }

            LineStackModel model = new LineStackModel();
            model.Series = new List<LineStackSeriesModel>();
            model.Legend = "['票重',  '验收量', '车数']";
            model.xAxisData = Newtonsoft.Json.JsonConvert.SerializeObject(xAxisList);

            LineStackSeriesModel Series = new LineStackSeriesModel();
            Series.name = "票重";
            Series.data = Newtonsoft.Json.JsonConvert.SerializeObject(BlanceList);
            model.Series.Add(Series);

            Series = new LineStackSeriesModel();
            Series.name = "验收量";
            Series.data = Newtonsoft.Json.JsonConvert.SerializeObject(BillWeightList);
            model.Series.Add(Series);

            Series = new LineStackSeriesModel();
            Series.name = "车数";
            Series.data = Newtonsoft.Json.JsonConvert.SerializeObject(CarCountList);
            model.Series.Add(Series);
            return View(model);
        }

        public ActionResult LineStack3(string LPR, int QueryYear, int QueryMonth)
        {
            var list = homeBLL.QueryRCMDetailChart(QueryYear, QueryMonth, LPR);

            DateTime dtStartDate = Convert.ToDateTime(string.Format("{0}-{1}-01", QueryYear, QueryMonth));
            DateTime dtEndDate = Convert.ToDateTime(string.Format("{0}-{1}-01", QueryYear, QueryMonth + 1));

            List<string> xAxisList = new List<string>();
            List<object> BlanceList = new List<object>();
            List<object> BILLWEIGHTList = new List<object>();
            List<object> NETList = new List<object>();
            List<object> TAREList = new List<object>();

            while (dtStartDate < dtEndDate)
            {
                //DateTime dtStartTime = Convert.ToDateTime(dtStartDate.ToString("yyyy-MM-dd 00:00:00"));
                //DateTime dtEndTime = Convert.ToDateTime(dtStartDate.ToString("yyyy-MM-dd 23:59:59"));
                int QueryDay = dtStartDate.Day;
                xAxisList.Add(dtStartDate.Day.ToString());
                var temp = (from u in list where u.STATS_DAY == QueryDay select u).ToList();
                if (temp != null && temp.Count > 0)
                {
                    decimal dBlance = 0;
                    decimal dWeight = 0;
                    decimal dCoffecent = 0;
                    decimal dTare = 0;
                    foreach (var item in temp)
                    {
                        dBlance += item.BALANCE;
                        dWeight += item.BILL_WEIGHT == null ? 0 : Convert.ToDecimal(item.BILL_WEIGHT);
                        dCoffecent += (decimal)item.NET;
                        dTare += item.TARE;
                    }

                    BlanceList.Add(dBlance);
                    BILLWEIGHTList.Add(dWeight);
                    NETList.Add(dCoffecent);
                    TAREList.Add(dTare);
                }
                else
                {
                    BlanceList.Add("");
                    BILLWEIGHTList.Add("");
                    NETList.Add("");
                    TAREList.Add("");
                }
                dtStartDate = dtStartDate.AddDays(1);
            }

            LineStackModel model = new LineStackModel();
            model.Series = new List<LineStackSeriesModel>();
            model.Legend = "['验收量', '票重', '净重', '皮重']";
            model.xAxisData = Newtonsoft.Json.JsonConvert.SerializeObject(xAxisList);

            LineStackSeriesModel Series = new LineStackSeriesModel();
            Series.name = "验收量";
            Series.data = Newtonsoft.Json.JsonConvert.SerializeObject(BlanceList);
            model.Series.Add(Series);

            Series = new LineStackSeriesModel();
            Series.name = "票重";
            Series.data = Newtonsoft.Json.JsonConvert.SerializeObject(BILLWEIGHTList);
            model.Series.Add(Series);

            Series = new LineStackSeriesModel();
            Series.name = "净重";
            Series.data = Newtonsoft.Json.JsonConvert.SerializeObject(NETList);
            model.Series.Add(Series);

            Series = new LineStackSeriesModel();
            Series.name = "皮重";
            Series.data = Newtonsoft.Json.JsonConvert.SerializeObject(TAREList);
            model.Series.Add(Series);

            return View(model);
        }
    }
}